package mutris.client.com;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import mutris.client.MutrisGame;

public class AppMenuBar extends JMenuBar implements Observer {

	
	private MutrisGame game;
	
	
	private JMenuItem mnuItemNewSingle;
	private JMenuItem mnuItemStopGame;
	
	
	public AppMenuBar(MutrisGame game) {
		super();
		
		init();
		
		setGame(game);
	}
	
	
	private void init() {
		JMenu mnuGame = new JMenu("Game");
		
		mnuItemNewSingle = new JMenuItem("New single game");
		mnuItemNewSingle.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				game.start();
			}
		});
		mnuGame.add(mnuItemNewSingle);
		
		
		mnuItemStopGame = new JMenuItem("Stop game");
		mnuItemStopGame.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				game.end();
			}
		});
		mnuGame.add(mnuItemStopGame);
		
		add(mnuGame);

		
		
		JMenu mnuOptions = new JMenu("Options");
		add(mnuOptions);
	}
	
	
	public MutrisGame getGame() {
		return game;
	}
	
	public void setGame(MutrisGame game) {
		if (null != this.game) {
			this.game.getBoard().deleteObserver(this);
		}
		
		this.game = game;
		
		if (null != this.game) {
			this.game.getBoard().addObserver(this);
		}
		
		updateMenus();
	}

	
	private void updateMenus() {
		if (null != game) {
			mnuItemNewSingle.setEnabled(!game.isStarted());
			mnuItemStopGame.setEnabled(game.isStarted());
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		//if (null != arg) {
			updateMenus();
		//}
	}
	
}
